package com.java.zhuiyun.api.pictureidiomquestion.service.impl;

import com.java.zhuiyun.api.image_file.service.ImageFileService;
import com.java.zhuiyun.api.pictureidiomlevel.model.PictureIdiomLevelModel;
import com.java.zhuiyun.api.pictureidiomquestion.dao.PictureIdiomQuestionMapper;
import com.java.zhuiyun.api.pictureidiomquestion.entity.PictureIdiomQuestion;
import com.java.zhuiyun.api.pictureidiomquestion.model.PictureIdiomQuestionModel;
import com.java.zhuiyun.api.pictureidiomquestion.service.PictureIdiomQuestionService;
import com.java.zhuiyun.common.response.CommonResult;
import com.java.zhuiyun.util.pageUtil.MybatisPageHelper;
import com.java.zhuiyun.util.pageUtil.PageRequest;
import com.java.zhuiyun.util.pageUtil.PageResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName PictureIdiomQuestionServiceImpl
 * @Description TODO
 * @Author MJL
 * @Date 2023/9/14 16:58
 **/
@Service
public class PictureIdiomQuestionServiceImpl implements PictureIdiomQuestionService {
    @Resource
    PictureIdiomQuestionMapper pictureIdiomQuestionMapper;
    @Resource
    ImageFileService imageFileService;
    /**
    * @Author MJL
    * @Description todo 添加看图猜成语题库
    * @Date 2023/9/14 17:07
    * @Return  * @param null
    */
    @Override
    public CommonResult addPictureIdiomQuestion(MultipartFile file, PictureIdiomQuestionModel pictureIdiomQuestionModel) {
        int idiomImgId = Integer.parseInt(imageFileService.addImageFile(file));
        if (idiomImgId > 0) {
            pictureIdiomQuestionModel.setIdiomImgId(idiomImgId);
        }
//        System.out.println("数据 " + idiomImgId);
        // 添加时间
        pictureIdiomQuestionModel.setCreateTime(new Date());
        // 修改时间
        pictureIdiomQuestionModel.setUpdateTime(new Date());
        // 删除标识
        pictureIdiomQuestionModel.setDelFlag(0);
        int i = pictureIdiomQuestionMapper.addPictureIdiomQuestion(pictureIdiomQuestionModel);
        if (i>0){
            return CommonResult.ok("添加成功");
        } else if (i<0){
            return CommonResult.ok("添加失败");
        }
        return CommonResult.ok("成功");
    }
    /**
    * @Author MJL
    * @Description todo 查询所有成语信息
    * @Date 2023/9/14 23:29
    * @Return  * @param null
    */
    @Override
    public PageResult queryPictureIdiomQuestion(PictureIdiomQuestionModel pictureIdiomQuestionModel, PageRequest pageRequest) {
        MybatisPageHelper.startPage(pageRequest);
        PageResult pageResult = MybatisPageHelper.getPageResult(pictureIdiomQuestionMapper.queryPictureIdiomQuestion(pictureIdiomQuestionModel));
        return pageResult;
    }
    /**
    * @Author MJL
    * @Description todo 查询出当前成语的错别字
    * @Date 2023/9/15 9:59
    * @Return  * @param null
    */
    @Override
    public List<PictureIdiomQuestion> queryPictureIdiomQuestionTypos(PictureIdiomQuestion pictureIdiomQuestion) {
        List<String> aaa = new ArrayList<>();
        aaa.add(pictureIdiomQuestion.getPictureTypos());
        System.out.println("数据 " + aaa);
        return null;
    }

    @Override
    public List<PictureIdiomQuestion> getByIdIdiom(Integer id) {
        return pictureIdiomQuestionMapper.getByIdIdiom(id);
    }
    /**
    * @Author MJL
    * @Description todo 修改成语信息
    * @Date 2023/10/11 20:04
    * @Return  * @param null
    */
    @Override
    public CommonResult updatePictureIdiom(MultipartFile file, PictureIdiomQuestionModel pictureIdiomQuestionModel) {
        int idiomImgId = Integer.parseInt(imageFileService.addImageFile(file));
        if (idiomImgId > 0) {
            pictureIdiomQuestionModel.setIdiomImgId(idiomImgId);
        }
        // 修改时间
        pictureIdiomQuestionModel.setUpdateTime(new Date());
        int i = pictureIdiomQuestionMapper.updatePictureIdiom(pictureIdiomQuestionModel);
        if (i > 0) {
            return CommonResult.ok("修改成功");
        } else {
            return CommonResult.error("修改失败");
        }
    }
    /**
    * @Author MJL
    * @Description todo 根据当前关卡查询出成语信息
    * @Date 2023/10/13 13:33
    * @Return  * @param null
    */
    @Transactional
    @Override
    public List<PictureIdiomQuestion> getByIdIdiomByLevel(Integer level) {
        return pictureIdiomQuestionMapper.getByIdIdiomByLevel(level);
    }
    /**
    * @Author MJL
    * @Description todo 看图猜成语删除成语
    * @Date 2023/10/16 9:58
    * @Return  * @param null
    */
    @Override
    public CommonResult deletePictureIdiom(String id) {
        Integer i = pictureIdiomQuestionMapper.deletePictureIdiom(id);
        if (i>0) {
            return CommonResult.ok("删除成功");
        } else {
            return CommonResult.error("删除失败");
        }
    }
    /**
    * @Author MJL
    * @Description todo 查询当前关卡成语数量
    * @Date 2023/10/16 19:57
    * @Return  * @param null
    */
    @Override
    public List<PictureIdiomLevelModel> queryLevelNum(Integer id) {
        List<PictureIdiomLevelModel> list = pictureIdiomQuestionMapper.queryLevelNum(id);
        return list;
    }
    /**
    * @Author MJL
    * @Description todo 查询关卡下拉框
    * @Date 2023/10/17 11:09
    * @Return  * @param null
    */
    @Override
    public List<PictureIdiomLevelModel> queryLevel(Integer id) {
        return pictureIdiomQuestionMapper.queryLevel(id);
    }
    /**
    * @Author MJL
    * @Description todo 删除成语
    * @Date 2023/10/22 18:41
    * @Return  * @param null
    */
    @Override
    public CommonResult deletePictureIdioms(Integer id) {
        Integer i = pictureIdiomQuestionMapper.deletePictureIdioms(id);
        if (i>0) {
            return CommonResult.ok("删除成功");
        } else {
            return CommonResult.error("删除失败");
        }
    }
}
